package com.liyu.listener;

import cn.hutool.core.date.DateUtil;
import com.liyu.config.DlxComment;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * @author 李誉
 * @date 2022-07-06 23:01
 * @description
 */
@Slf4j
@Component
public class DlxConsumer {

    /**
     * 普通队列消费者
     */
    @RabbitListener(queues = DlxComment.NORMAL_QUEUE)
    public void normalConsume(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody(), StandardCharsets.UTF_8);
        channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
        log.info("普通队列{}在{}否定应答了一条消息：{}", DlxComment.NORMAL_QUEUE, DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"), msg);
    }

    /**
     * 死信消费者
     */
    @RabbitListener(queues = DlxComment.DLX_QUEUE)
    public void dlxConsume(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody(), StandardCharsets.UTF_8);
        channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
        log.info("死信队列{}在{}消费了一条消息：{}", DlxComment.DLX_QUEUE, DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"), msg);
    }
}
